package com.apestech.framework.xml.plugin.modul;

import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.json.JSONOrder;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.xml.sql.modul.QueryhTw;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2018-12-12-18:00
 */
public class GridHead extends StringUtil{

    public class PivotHead {
        private String field;
        private String title;
        private Object value;

        public PivotHead(String field, String title, Object value){
            this.field=field;
            this.title=title;
            this.value=value;
        }

        public String getField() {
            return field;
        }

        public Object getValue() {
            return value;
        }

        public String getTitle() {
            return title;
        }
    }

    private JSONArray head;
    private JSONOrder headjson;
    private JSONOrder pivotjson;
    private JSONOrder pivotdata;
    private JSONObject pivotmap;
    private ArrayList<PivotHead> pivotlist;
    private boolean pivotNumber=false;

    public void addHead(JSONObject json) throws Exception {
        if(headjson==null) headjson=new JSONOrder();
        String field=json.getString("field");
        headjson.put(field, json);
    }

    public void addHead(ArrayList<String> list) throws Exception {
        if(list==null) return;
        int len=list.size();
        for(int i=0; i<len; i++){
            String k=list.get(i);
            addHead(new JSONObject("{\"field\":\""+k+"\",\"title\":\""+k+"\"}"));
        }
    }

    public void addHead(JSONArray head) throws Exception {
        if(head==null) return;
        int len=head.size();
        for(int i=0; i<len; i++){
            addHead(JSON.toJSON(head.get(i)));
        }
    }

    public void addPivot(JSONObject json) throws Exception {
        if(pivotjson==null) pivotjson=new JSONOrder();
        String id=json.getString("id");
        if(json.get("text")==null) json.put("text", id);
        pivotjson.put(id, json);
    }

    public void addPivot(JSONArray pivot) throws Exception {
        if(pivot==null) return;
        int len=pivot.size();
        for(int i=0; i<len; i++){
            addPivot((JSONObject) pivot.get(i));
        }
    }

    public void addPivotField(SqlResultSet rs, QueryhTw htw) throws Exception {
        head=new JSONArray();
        if(headjson.get(htw.getDataField())==null) addHead(new JSONObject("{\"field\":\""+htw.getDataField()+"\"}"));
        ArrayList<String> hkeylist=headjson.getOrderKeylist();
        int len=hkeylist.size();
        for(int i=0; i<len; i++){
            String key=hkeylist.get(i);
            if(!(htw.getDataField().equals(key) || htw.isPivotField(key))){
                head.add(headjson.get(key));
            } else if(htw.getDataField().equals(key)){
                getPivotDataField(rs, htw);
                if(pivotjson==null) setPivotCreateHead();
                else setPivotDataHead(htw);
            }
        }
    }

    private void setPivotCreateHead(){
        ArrayList<String> klist=pivotdata.getOrderKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String key=klist.get(i);
            String mkey=pivotmap.getString(key);
            JSONObject kjson=new JSONObject();
            if(headjson.get(mkey)!=null) kjson.putAll((JSONObject) headjson.get(mkey));
            kjson.put("field", key);
            kjson.put("title", pivotdata.getString(key));
            head.add(kjson);
        }
    }

    private void setPivotDataHead(QueryhTw htw) throws Exception {
        ArrayList<String> klist=pivotjson.getOrderKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String key=klist.get(i);
            JSONObject m= (JSONObject) pivotjson.get(key);
            String sid=m.getString("id");
            Object id = null;
            if(isNumber(sid)) id=castToDouble(sid);
            else id=sid;
            String field = null;
            if (!pivotNumber) field = sid.toUpperCase();
            else field = htw.getDataField() + "_" + sid;

            ArrayList<String> plist = htw.getPivotlist().getOrderKeylist();
            int plen = plist.size();
            for (int p = 0; p < plen; p++) {
                String f = plist.get(p);  //查询结果下的正常字段
                String pfield = field + "_" + f;  //横转竖之后的新字段
                if(pivotdata.get(pfield)!=null){
                    JSONObject kjson=new JSONObject();
                    if(headjson.get(f)!=null) kjson.putAll((JSONObject) headjson.get(f));
                    kjson.put("field", pfield);
                    kjson.put("title", pivotdata.getString(pfield));
                    head.add(kjson);
                }
            }
        }
    }

    private void getPivotDataField(SqlResultSet rs, QueryhTw htw) throws Exception {
        pivotdata = new JSONOrder();
        pivotmap = new JSONObject();
        pivotlist = new ArrayList<PivotHead>();
        while(rs.next()) {
            String field = null;
            String title = null;
            Object value = null;
            if (htw.getTable() == null) {
                value = rs.getObject(htw.getDataField());
                field = htw.getDataField();
                String k=castToString(value);
                if (pivotjson != null) {
                    if(pivotjson.get(k)!=null) title = ((JSONObject)pivotjson.get(k)).getString("text");
                    else addPivot(new JSONObject("{\"id\":\""+k+"\",\"text\":\""+k+"\"}"));
                }
            } else {
                value = rs.getObject(htw.getIdField());
                title = rs.getString(htw.getNameField());
                field = htw.getIdField();
                String k=castToString(value);
                if(pivotjson!=null && pivotjson.get(k)==null) addPivot(new JSONObject("{\"id\":\""+k+"\",\"text\":\""+k+"\"}"));
            }
            if (value instanceof String){
                pivotNumber=false;
                field = ((String) value).toUpperCase();
            }
            else{
                pivotNumber=true;
                field = field + "_" + castToString(value);
            }
            if (title == null) title = field;

            ArrayList<String> plist = htw.getPivotlist().getOrderKeylist();
            int len = plist.size();
            for (int i = 0; i < len; i++) {
                String f = plist.get(i);  //查询结果下的正常字段
                String key = field + "_" + f;  //横转竖之后的新字段
                if (len == 1) pivotdata.put(key, title);
                else{
                    String text=((JSONObject)headjson.get(f)).getString("title");
                    pivotdata.put(key, title + text);
                }
                ArrayList<String> list=null;
                if(pivotmap.get(f)!=null) list= (ArrayList<String>) pivotmap.get(f);
                else list=new ArrayList<String>();
                list.add(key);
                pivotmap.put(f, list);
                pivotmap.put(key,f);
            }
            pivotlist.add(new PivotHead(field,title,value));
        }
    }

    private void setHeadData(){
        if(head!=null) return;
        if(headjson==null) return;
        head=new JSONArray();
        ArrayList<String> klist=headjson.getOrderKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String key=klist.get(i);
            if(headjson.get(key) instanceof JSONOrder) head.add(((JSONOrder)headjson.get(key)).getJson());
            else head.add(headjson.get(key));
        }
    }

    public JSONArray getHead() {
        if(head==null) setHeadData();
        return head;
    }

    public JSONObject getPivotmap() {
        return pivotmap;
    }

    public JSONOrder getHeadjson() {
        return headjson;
    }

    public ArrayList<PivotHead> getPivotlist() {
        return pivotlist;
    }
}
